package com.example.gymmanagebackend.controller;

import com.example.gymmanagebackend.Result;
import com.example.gymmanagebackend.pojo.Members;
import com.example.gymmanagebackend.pojo.User;
import com.example.gymmanagebackend.service.MembersService;
import com.example.gymmanagebackend.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MembersService membersService;

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody User user) {
        System.out.println("Received login request: " + user);
        User authenticatedUser = userService.login(user.getUsername(), user.getPassword());
        if (authenticatedUser != null) {
            System.out.println("User authenticated successfully");
            if ("ad".equals(user.getIdentify())) {
                // ad身份直接返回认证成功的用户信息
                return ResponseEntity.ok(authenticatedUser);
            } else if ("mem".equals(user.getIdentify())) {
                // mem身份直接返回认证成功的用户信息
                return ResponseEntity.ok(authenticatedUser);
            } else {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("不支持的身份类型");
            }
        } else {
            System.out.println("Authentication failed");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
        }
    }


    @PostMapping("/add")
    public ResponseEntity<?> addUser(@RequestBody User user) {
        int result = userService.addUser(user);
        if (result > 0) {
            return ResponseEntity.ok().body("用户添加成功");
        } else {
            return ResponseEntity.badRequest().body("用户添加失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> deleteUserById(@PathVariable Integer id) {
        int result = userService.deleteUserById(id);
        if (result > 0) {
            return ResponseEntity.ok().body("用户删除成功");
        } else {
            return ResponseEntity.badRequest().body("用户删除失败");
        }
    }

    @PutMapping("/update")
    public ResponseEntity<?> updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        if (result > 0) {
            return ResponseEntity.ok().body("用户更新成功");
        } else {
            return ResponseEntity.badRequest().body("用户更新失败");
        }
    }

    @GetMapping("/all")
    public ResponseEntity<?> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseEntity.ok().body(users);
    }

    @GetMapping("/{username}")
    public ResponseEntity<?> findUserByUsername(@PathVariable String username) {
        User user = userService.findUserByUsername(username);
        if (user != null) {
            return ResponseEntity.ok().body(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/resetPassword")
    public ResponseEntity<?> resetPassword(@RequestBody Map<String, String> payload) {
        String userName = payload.get("userName");
        String password = payload.get("password");

        User user = userService.findUserByUsername(userName);
        if (user != null) {
            user.setPassword(password);
            int result = userService.updateUser(user);
            if (result > 0) {
                return ResponseEntity.ok(new Result(true, "密码重置成功"));
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(false, "密码重置失败"));
            }
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new Result(false, "用户信息未找到"));
        }


    }
}
